Skip to content

Conversation

antonilol
Copy link
Contributor

@antonilol antonilol commented Sep 21, 2025

ACP: rust-lang/libs-team#658
Tracking issue: #146975

Text below was written before opening the ACP

Feature was requested in #69939, I recently also needed it so decided to implement it as my first contribution to the Rust standard library. I plan on doing more but wanted to start with a small change.

Some questions I had (both on implementation and design) with answers:

  • Q: extend allows iterators that yield &T where T is Clone, should extend_front do too?
    A: No, users can use copied and/or cloned.
  • Q: Does this need a whole new trait like Extend or only a method on VecDeque?
    A: No, see ACP.
  • Q: How do I deal with all the code duplication? Most code is similar to that of extend, maybe there is a nice way to factor out the code around push_unchecked/push_front_unchecked.
    Will come back to this later.
  • Q: Why are certain things behind feature gates, cfg(not(test)) like vec::IntoIter here and cfg(not(no_global_oom_handling)) like Vec::extend_from_within? (I am also looking at implementing VecDeque::extend_from_within)
    A: See add extend_front to VecDeque with specialization like extend (unfinished) #146861 (review)
  • Q: Should extend_front act like repeated pushes to the front of the queue? This reverses the order of the elements. Doing it different might incur an extra move if the iterator length is not known up front (where do you start placing elements in the buffer?).
    A: extend_front acts like repeated pushes, prepend preserves the element order, see ACP or tracking issue.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 21, 2025
@workingjubilee workingjubilee added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-acp This change is blocked on the author creating an ACP. labels Sep 21, 2025
@workingjubilee
Copy link
Member

workingjubilee commented Sep 21, 2025

cc @the8472 a bunch of questions re: specialization that I believe you may be able to address

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@the8472 the8472 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are certain things behind feature gates, cfg(not(test))

This is due to tests living in the alloctest crate and some of the tests need access to implementation details so they're done with #[path] module imports there which means the originals need to be disabled.
See #136642

cfg(not(no_global_oom_handling)) like

This one is an unstable feature previously used by the linux kernel. IIRC a few other projects are using it too. It's supposed to remove all methods that abort on oom rather than returning an error from alloc's API surface.

Feature was requested in #69939

It's recommended to file an API change proposal to get some preliminary review of the proposed API from the team.

Considering that there are open questions how the API should look like I think it'd be best to have that discussion on an ACP thread so we can focus on the implementation details there.

View changes since this review

}

#[cfg(not(test))]
impl<T, A: Allocator> SpecExtendFront<T, Rev<vec::IntoIter<T>>> for VecDeque<T, A> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a curious specialization. Is the expectation that people will frequently use Rev to prepend to a vecdeque?

Anyway, specializations should have dedicated tests since they're not covered by the regular ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a curious specialization. Is the expectation that people will frequently use Rev to prepend to a vecdeque?

This depends on the design of extend_front, if it will act like repeated push_front, specializing this can have significant performance improvements. This would be used when prepending elements from a Vec into a VecDeque, in original order, a bit like the requested VecDeque::prepend (#69939 (comment)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prepend (see tracking issue) will use this, it calls self.extend_front(other.into_iter().rev())

@the8472
Copy link
Member

the8472 commented Sep 21, 2025

cc @the8472 a bunch of questions re: specialization that I believe you may be able to address

I don't think any of the questions are specific to specialization?

@rust-log-analyzer

This comment has been minimized.

Comment on lines +193 to +197
// impl<T, A: Allocator> SpecExtendFront<T, Rev<Copied<slice::Iter<'_, T>>>> for VecDeque<T, A>
// where
// T: Copy,
// {
// #[track_caller]
// fn spec_extend_front(&mut self, iter: Rev<Copied<slice::Iter<'_, T>>>) {
// unsafe { prepend(self, iter.into_inner().it.as_slice()) };
// }
// }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to add specialization for deque.extend_front(slice.iter().rev()) where slice is &[T] with T: Copy (thus also deque.prepend(slice.iter())) but got stuck here. Copied's inner iterator can not be accessed (I made it pub while trying) and I get the following error: "cannot specialize on trait Clone". Any ideas on if and how this is possible? What does Extend do different so that it can specialize on Copy?

@rust-log-analyzer

This comment has been minimized.

@antonilol antonilol force-pushed the vec_deque_extend_front branch 2 times, most recently from 22995ae to 552e6f1 Compare September 29, 2025 19:34
@antonilol antonilol force-pushed the vec_deque_extend_front branch from 5c9b311 to d4ae120 Compare September 30, 2025 20:36
@antonilol antonilol marked this pull request as ready for review September 30, 2025 20:36
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 30, 2025
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 30, 2025

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-acp This change is blocked on the author creating an ACP. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants